<template>
  <div class="login_container">
    <!-- 登录表单 -->
    <div class="login_box">
      <div class="avatar_box">
        <img src="~assets/img/logo.png" alt="avatar" />
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="60px"
        class="login_form"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
export default {
  name: "Login",
  data() {
    return {
      // 表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    login(){
       this.$refs.loginFormRef.validate(async(valid)=>{
         if(!valid){return false}
        const {data: res}=await this.$http.post('login',this.loginForm)
        if(res.meta.status!=200){return this.$message.error('登录失败,账户名或密码不正确')}
        this.$message.success('成功登录')
        // console.log(res.data);
        // 1.登陆成功后的taken保存在sessionStorage(打开期间有效)中
        window.sessionStorage.setItem("token",res.data.token)
        // 2.通过编程式导航跳转到后台页面/Home
        this.$router.push("/Home")
       })
    },
    resetLoginForm(){
       this.$refs.loginFormRef.resetFields()
    }
  }
};
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
    .login_box {
  width: 450px;
  height: 260px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: rgb(255, 255, 255);

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -120%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
}
</style>